package com.oyjd.fw.log.action;

import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.fw.action.BaseAction;
import com.oyjd.fw.log.service.LogService;

/**
 * 日志下载Action层
 * 
 * @author 欧建东
 * @version 1.0
 */
@Action(value = "sysLog")
public class LogAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8358935529568852782L;
	private LogService downLogService;

	@Autowired
	public void setDownLogService(LogService downLogService) {
		this.downLogService = downLogService;
	}

	/**
	 * 获取下载列表
	 * 
	 * @return
	 */
	public void getList() {
		try {
			String root = ServletActionContext.getServletContext().getRealPath("");
			String row = "<td><a class='edit-font' href=\"sysLog!downLog.action?id={0}\">{1}</a></td>";
			String logList = downLogService.getList(root, row);
			PrintWriter pw = getResponse().getWriter();
			String c = getJspModle(logList);
			pw.write(c);
			pw.close();
		} catch (Exception e) {
		}
	}

	/**
	 * 获取下载文件
	 */
	public void downLog() {
		try {
			String id = ServletRequestUtils.getStringParameter(getRequest(), "id");
			downLogService.downLog(id, getResponse());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 清除日志
	 */
	public void deleteLogs() {
		try {
			String root = ServletActionContext.getServletContext().getRealPath("");
			downLogService.deleteLogs(root);
			getList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取日志页面模板
	 * 
	 * @return
	 */
	private String getJspModle(String s) {
		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
		sb.append("<html>\r\n");
		sb.append("	<head>\r\n");
		sb.append("		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
		sb.append("		<link rel=\"stylesheet\" type=\"text/css\" href=\"" + getRequest().getContextPath() + "/css/css.css\"/>\r\n");
		sb.append("		<title>下载日志文件</title>\r\n");
		sb.append("	</head>\r\n");
		sb.append("	<body class='edit-bgr-color'>\r\n");
		sb.append("		<center>\r\n");
		sb.append("			<div class=\"right-head\"><span class=\"right-head-font\">系统日志</span></div>\r\n");
		sb.append("			<table border=\"0\" cellspacing=\"4\" cellpadding=\"1\" width=\"90%\">\r\n");
		sb.append("				<tr align='left'><th width='25%'>错误级</th><th width='25%'>警告级</th><th width='25%'>信息级</th><th width='25%'>测试级</th></tr>\r\n");
		sb.append("			</table>\r\n");
		sb.append("			<hr width=\"100%\" />\r\n");
		sb.append("			<table border=\"0\" cellspacing=\"4\" cellpadding=\"1\" width=\"90%\">\r\n");
		sb.append("				").append(s).append("\r\n");
		sb.append("			</table>\r\n");
		sb.append("			<hr width=\"100%\" />\r\n");
		sb.append("			<a class='edit-font' href=\"javascript:if(confirm('确定要清除历史日志吗？')){location.href='sysLog!deleteLogs.action';}\">清除历史日志</a>\r\n");
		sb.append("		</center>\r\n");
		sb.append("	</body>\r\n");
		sb.append("</html>\r\n");
		return sb.toString();
	}

}
